package com.aitoedu.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aitoedu.server.entity.Chapter;
import com.aitoedu.server.service.ChapterService;
import com.aitoedu.server.mapper.ChapterMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author 曹正豪
* @description 针对表【chapter(章节信息)】的数据库操作Service实现
* @createDate 2024-10-15 08:40:04
*/
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter>
    implements ChapterService{

    @Autowired
    private ChapterMapper chapterMapper;

    @Override
    public List<Chapter> KGK(Long courseId) {
        List<Chapter> chapters = chapterMapper.selectList(new QueryWrapper<Chapter>().eq("cid",courseId));
        List<Chapter> level = chapters.stream()
                .filter(chapter -> chapter.getParentId()==0
                ).map(chapter -> {
                  chapter.setChildren(getChildrens(chapter,chapters));
                  return chapter;
                }).collect(Collectors.toList());
        return level;
    }

    private List<Chapter> getChildrens(Chapter root,List<Chapter> all){
        List<Chapter> children = all.stream().filter(chapter -> {
            return chapter.getParentId() == root.getId();
        }).map(chapter -> {
            chapter.setChildren(getChildrens(chapter, all));
            return chapter;
        }).collect(Collectors.toList());
        return children;
    }
}




